package com.zhongli.devplatform.controller.live;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.dto.user.VerifiedDTO;
import com.zhongli.devplatform.po.user.ReadUserInfoPO;
import com.zhongli.devplatform.service.imp.user.ReadUserInfoService;
import com.zhongli.devplatform.service.imp.live.TencentLive;
import com.zhongli.devplatform.dto.LiveDTO;
import com.zhongli.devplatform.service.imp.live.ReadLiveRoomService;
import com.zhongli.devplatform.vo.live.LiveCloseVO;
import com.zhongli.devplatform.vo.live.LiveOpenVO;
import com.zhongli.devplatform.vo.live.LiveVO;
import com.zhongli.devplatform.bean.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author wy
 * @since  2020/8/5
 */
@Api(tags = "直播", description = "直播，操作")
@RestController
@RequestMapping("/live")
public class LiveController {


    @Autowired
    private TencentLive tencentLive;

    @Autowired
    private ReadLiveRoomService liveRoomService;

    @Autowired
    private ReadUserInfoService userInfoService;



    @ApiOperation("检查是否实名认证")
    @GetMapping("/checkedVerified")
    public Res checkedVerified(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        ReadUserInfoPO userInfo = userInfoService.getOne(new QueryWrapper<ReadUserInfoPO>().eq("user_id", currentUserId));
        if (StringUtils.hasLength(userInfo.getRealName()) && StringUtils.hasLength(userInfo.getIdCard()))
            return Res.not_ok("请完成实名认证");
        return Res.ok("success");
    }


    @ApiOperation("第一次 进入直播模块 实名认证")
    @PostMapping("/verified")
    public Res verified(@RequestBody VerifiedDTO verifiedDTO){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        ReadUserInfoPO userInfo = userInfoService.getOne(new QueryWrapper<ReadUserInfoPO>().eq("user_id", currentUserId));
        userInfo.setRealName(verifiedDTO.getRealName());
        userInfo.setIdCard(verifiedDTO.getIdCard());
        userInfoService.save(userInfo);
        return Res.ok("success");
    }


    @ApiOperation("开启直播间 或者第一次创建直播间")
    @PostMapping("/startLiveBroadcast")
    public Res startLiveBroadcast(@RequestBody LiveDTO liveDTO){
        LiveOpenVO live = liveRoomService.createLive(liveDTO);
        return Res.ok(live);
    }


    @ApiOperation("关闭直播间 断开推流")
    @PostMapping("/closeLiveBroadcast")
    public Res closeLiveBroadcast(){
        LiveCloseVO live = liveRoomService.closeLiveBroadcast();
        return Res.ok(live);
    }


    @ApiOperation("获取全部的在线的直播间")
    @GetMapping("/getAllOnlineLive")
    public Res getAllOnlineLive(){
        List<LiveVO> data = liveRoomService.getAllOnlineLive();
        return Res.ok(data);
    }






}
